package com.springboot.biz.impl;

import cn.hutool.core.convert.Convert;
import com.springboot.biz.ItemBiz;
import com.springboot.biz.NoteBiz;
import com.springboot.biz.PipeBiz;
import com.springboot.dao.PipeDao;
import com.springboot.entity.*;
import com.springboot.util.AppHelper;
import com.springboot.util.CameHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@Transactional
public class PipeBizImpl implements PipeBiz {

    @Resource
    private PipeDao pipeDao;
    @Resource
    private ItemBiz itemBiz;
    @Resource
    private NoteBiz noteBiz;

    public void insertPipe(Pipe pipe) {
        pipeDao.insertPipe(pipe);
    }

    public void updatePipe(Pipe pipe) {
        pipeDao.updatePipe(pipe);
    }

    public void deletePipe(Pipe pipe) {
        pipeDao.deletePipe(pipe);
    }

    public void combinPipe(int id, List<?> list) {
        Map<String, Object> map = AppHelper.getMap("id", id, "list", list);
        pipeDao.combinPipe(map);
    }

    public Pipe findInfoPipe(int id, User user) {
        Map<String, Object> map = AppHelper.getMap("id", id, "user", user);
        return pipeDao.findInfoPipe(map);
    }

    public Pipe findInfoPipe(Map<String, Object> map) {
        return pipeDao.findInfoPipe(map);
    }

    public List<Pipe> findListPipe(Project project) {
        Map<String, Object> map = AppHelper.getMap("project", project);
        return pipeDao.findListPipe(map);
    }

    public List<Pipe> findListPipe(Map<String, Object> map) {
        return pipeDao.findListPipe(map);
    }

    public void sortPipe(int id) {
        pipeDao.sortPipe(id);
    }

    public int getCount(Map<String, Object> map) {
        return pipeDao.getCount(map);
    }

    public void check(Pipe cust, Pipe pipe) {
        StringBuffer context = new StringBuffer("");
        if (cust.getNo() != pipe.getNo())
            context.append("*.修改Survey ID为：" + pipe.getNo() + "\n");
        if (!isEquals(cust.getOperator(), pipe.getOperator()))
            context.append("*.修改Operator为：" + pipe.getOperator() + "\n");
        if (!isEquals(cust.getWorkorder(), pipe.getWorkorder()))
            context.append("*.修改Work Order No为：" + pipe.getWorkorder() + "\n");
        if (!isEquals(cust.getReference(), pipe.getReference()))
            context.append("*.修改Pipe Length Reference为：" + pipe.getReference() + "\n");
        if (!isEquals(cust.getPurposes(), pipe.getPurposes()))
            context.append("*.修改Purpose为：" + pipe.getPurposes() + "\n");
        if (!isEquals(cust.getSlope(), pipe.getSlope()))
            context.append("*.修改Slope为：" + pipe.getSlope() + "\n");
        if (!isEquals(cust.getSloperef(), pipe.getSloperef()))
            context.append("*.修改Slope Reference No为：" + pipe.getSloperef() + "\n");
        if (!isEquals(cust.getYearlaid(), pipe.getYearlaid()))
            context.append("*.修改Year Laid为：" + pipe.getYearlaid() + "\n");
        if (!isEquals(cust.getDate(), pipe.getDate()))
            context.append("*.修改Date为：" + pipe.getDate() + "\n");
        if (!isEquals(cust.getTime(), pipe.getTime()))
            context.append("*.修改Time为：" + pipe.getTime() + "\n");

        if (!isEquals(cust.getDistrict1(), pipe.getDistrict1()))
            context.append("*.修改District1为：" + pipe.getDistrict1() + "\n");
        if (!isEquals(cust.getDistrict2(), pipe.getDistrict2()))
            context.append("*.修改District2为：" + pipe.getDistrict2() + "\n");
        if (!isEquals(cust.getDistrict3(), pipe.getDistrict3()))
            context.append("*.修改District3为：" + pipe.getDistrict3() + "\n");
        if (!isEquals(cust.getRoadname(), pipe.getRoadname()))
            context.append("*.修改Road Name为：" + pipe.getRoadname() + "\n");
        if (!isEquals(cust.getHousenum(), pipe.getHousenum()))
            context.append("*.修改House Number为：" + pipe.getHousenum() + "\n");
        if (!isEquals(cust.getBuilding(), pipe.getBuilding()))
            context.append("*.修改Building为：" + pipe.getBuilding() + "\n");
        if (!isEquals(cust.getDivision(), pipe.getDivision()))
            context.append("*.修改Division为：" + pipe.getDivision() + "\n");
        if (!isEquals(cust.getAreacode(), pipe.getAreacode()))
            context.append("*.修改Drainage Area Code为：" + pipe.getAreacode() + "\n");

        if (!isEquals(cust.getSmanholeno(), pipe.getSmanholeno()))
            context.append("*.修改Start MH为：" + pipe.getSmanholeno() + "\n");
        if (!isEquals(cust.getFmanholeno(), pipe.getFmanholeno()))
            context.append("*.修改Finish MH为：" + pipe.getFmanholeno() + "\n");
        if (!isEquals(cust.getUses(), pipe.getUses()))
            context.append("*.修改Use为：" + CameHelper.getCameText(pipe.getUses(), "use") + "\n");
        if (!isEquals(cust.getDire(), pipe.getDire()))
            context.append("*.修改Direction为：" + CameHelper.getCameText(pipe.getDire(), "dir") + "\n");
        if (!isEquals(cust.getHsize(), pipe.getHsize()))
            context.append("*.修改Size(Dia)H为：" + pipe.getHsize() + "\n");
        if (!isEquals(cust.getWsize(), pipe.getWsize()))
            context.append("*.修改Size(Dia)W为：" + pipe.getWsize() + "\n");
        if (!isEquals(cust.getShape(), pipe.getShape()))
            context.append("*.修改Shape为：" + CameHelper.getCameText(pipe.getShape(), "sha") + "\n");
        if (!isEquals(cust.getMater(), pipe.getMater()))
            context.append("*.修改Material为：" + CameHelper.getCameText(pipe.getMater(), "mat") + "\n");
        if (!isEquals(cust.getLining(), pipe.getLining()))
            context.append("*.修改Lining为：" + CameHelper.getCameText(pipe.getLining(), "lin") + "\n");
        if (cust.getPipelength() != pipe.getPipelength())
            context.append("*.修改Pipe Length为：" + pipe.getPipelength() + "\n");
        if (cust.getTestlength() != pipe.getTestlength())
            context.append("*.修改Total Length为：" + pipe.getTestlength() + "\n");

        if (!isEquals(cust.getSdepth(), pipe.getSdepth()))
            context.append("*.修改Start Depth为：" + pipe.getSdepth() + "\n");
        if (!isEquals(cust.getScoverlevel(), pipe.getScoverlevel()))
            context.append("*.修改Start Cover Level为：" + pipe.getScoverlevel() + "\n");
        if (!isEquals(cust.getSinvertlevel(), pipe.getSinvertlevel()))
            context.append("*.修改Start Invert Level为：" + pipe.getSinvertlevel() + "\n");
        if (!isEquals(cust.getFdepth(), pipe.getFdepth()))
            context.append("*.修改Finish Depth为：" + pipe.getFdepth() + "\n");
        if (!isEquals(cust.getFcoverlevel(), pipe.getFcoverlevel()))
            context.append("*.修改Finish Cover Level为：" + pipe.getFcoverlevel() + "\n");
        if (!isEquals(cust.getFinvertlevel(), pipe.getFinvertlevel()))
            context.append("*.修改Finish Invert Level为：" + pipe.getFinvertlevel() + "\n");
        if (!isEquals(cust.getCategory(), pipe.getCategory()))
            context.append("*.修改Category为：" + CameHelper.getCameText(pipe.getCategory(), "cat") + "\n");
        if (!isEquals(cust.getCleaned(), pipe.getCleaned()))
            context.append("*.修改Cleaned为：" + pipe.getCleaned() + "\n");
        if (!isEquals(cust.getWeather(), pipe.getWeather()))
            context.append("*.修改Weather为：" + CameHelper.getCameText(pipe.getWeather(), "wea") + "\n");
        if (!isEquals(cust.getVideono(), pipe.getVideono()))
            context.append("*.修改Video No/File Name为：" + pipe.getVideono() + "\n");
        if (!isEquals(cust.getComment(), pipe.getComment()))
            context.append("*.修改Comments General为：" + pipe.getComment() + "\n");

        List<Item> items1 = itemBiz.findListItem(cust);
        List<Item> items2 = pipe.getItems();
        for (int i = 0; i < items2.size(); i++) {
            Item item1 = items2.get(i);
            if (item1.getId() == 0) {
                context.append("*.新增管道记录>> Distance：" + item1.getDist() + "  \tCode：" + item1.getCode() + "\n");
                continue; // 继续
            }
            Item item2 = getItem(items1, item1.getId());
            if (item2 == null)
                continue; // 继续
            StringBuffer text = new StringBuffer("");
            if (Convert.toDouble(item1.getDist()) != Convert.toDouble(item2.getDist()))
                text.append("  - 修改Distance为：" + item1.getDist() + ";\n");
            if (!isEquals(item1.getCont(), item2.getCont()))
                text.append("  - 修改Cont/Def为：" + item1.getCont() + ";\n");
            if (!isEquals(item1.getCode(), item2.getCode()))
                text.append("  - 修改Code为：" + item1.getCode() + ";\n");
            if (!isEquals(item1.getDiam(), item2.getDiam()))
                text.append("  - 修改Dia./Dim为：" + item1.getDiam() + ";\n");
            if (!isEquals(item1.getClockAt(), item2.getClockAt()))
                text.append("  - 修改Clock At为：" + item1.getClockAt() + ";\n");
            if (!isEquals(item1.getClockTo(), item2.getClockTo()))
                text.append("  - 修改Clock To为：" + item1.getClockTo() + ";\n");
            if (!isEquals(item1.getPercent(), item2.getPercent()))
                text.append("  - 修改Intrusion %为：" + item1.getPercent() + ";\n");
            if (!isEquals(item1.getLengths(), item2.getLengths()))
                text.append("  - 修改Intrusion mm为：" + item1.getLengths() + ";\n");
            if (!isEquals(item1.getRemarks(), item2.getRemarks()))
                text.append("  - 修改Remarks为：" + item1.getRemarks() + ";\n");
            if (!isEquals(item1.getPicture(), item1.getPicture()))
                text.append("  - 修改记录截图;\n");
            if (!StringUtils.isEmpty(text.toString()))
                context.append("*.编辑管道记录列表第" + (i + 1) + "条记录：\n" + text);
        }
        // 保存日志
        if (!StringUtils.isEmpty(context.toString())) {
            User user = (User) AppHelper.findMap("user");
            Note note = new Note();
            note.setContext(context.toString());
            note.setDate(AppHelper.getDate(null));
            note.setUser(user);
            note.setPipe(pipe);
            noteBiz.insertNote(note);
        }
    }

    public int appendPipe(Pipe pipe) {
        insertPipe(pipe);
        String standard = pipe.getProject().getStandard();
        if (Objects.equals("HKCCEC 2009", standard)) {
            Item item1 = new Item();
            item1.setNo(0);
            item1.setDist("0.00");
            item1.setCode("ST");
            item1.setPipe(pipe);

            Item item2 = new Item();
            item2.setNo(1);
            item2.setDist("0.00");
            item2.setCode("MH");
            item2.setPipe(pipe);

            Item item3 = new Item();
            item3.setNo(2);
            item3.setDist("0.00");
            item3.setCode("WL");
            item3.setPipe(pipe);

            itemBiz.insertItem(item1);
            itemBiz.insertItem(item2);
            itemBiz.insertItem(item3);
        } else if (Objects.equals("MSCC 2004", standard)) {
            Item item1 = new Item();
            item1.setNo(0);
            item1.setDist("0.00");
            item1.setCode("MH");
            item1.setPipe(pipe);

            Item item2 = new Item();
            item2.setNo(1);
            item2.setDist("0.00");
            item2.setCode("WL C");
            item2.setPipe(pipe);

            itemBiz.insertItem(item1);
            itemBiz.insertItem(item2);
        } else if (Objects.equals("MSCC 2013", standard)) {
            Item item1 = new Item();
            item1.setNo(0);
            item1.setDist("0.00");
            item1.setCode("MH");
            item1.setPipe(pipe);

            Item item2 = new Item();
            item2.setNo(1);
            item2.setDist("0.00");
            item2.setCode("WLC");
            item2.setPipe(pipe);

            itemBiz.insertItem(item1);
            itemBiz.insertItem(item2);
        }
        return pipe.getId();
    }

    /**
     * 获取指定记录
     */
    private Item getItem(List<Item> list, int id) {
        return list.stream().filter(item -> item.getId() == id).findFirst().orElse(null);
    }

    /**
     * 判断字符串是否相等
     */
    private boolean isEquals(String text1, String text2) {
        String contexttext1 = StringUtils.isEmpty(text1) ? "" : text1;
        String contexttext2 = StringUtils.isEmpty(text2) ? "" : text2;
        return Objects.equals(contexttext1, contexttext2);
    }

}
